package com.mengchengnet.chaperonage.wxapi;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.mengchengnet.chaperonage.BaseActivity;
import com.mengchengnet.chaperonage.R;
import com.mengchengnet.chaperonage.util.Constants;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;


public class WXPayEntryActivity extends BaseActivity implements IWXAPIEventHandler{
	private Button btn;
	private TextView tv;
	private ImageView iv;
	private static final String TAG = "WXPayEntryActivity";
	private IWXAPI api;
	@Override
	protected void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay_result);
        toolbarState=TOOLBAR_STYLE_BACK_TITLE;
        initToolbar("支付结果");
         btn= (Button) findViewById(R.id.result_btn);
		tv= (TextView) findViewById(R.id.result_tv);
		iv= (ImageView) findViewById(R.id.result_imageview);
		api = WXAPIFactory.createWXAPI(this, Constants.APP_ID);
		api.handleIntent(getIntent(), this);
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
		api.handleIntent(intent, this);
	}

	@Override
	protected void onResume() {
		super.onResume();
	}



	@Override
	public void onReq(BaseReq baseReq) {

	}

	@Override
	public void onResp(BaseResp resp) {
		Log.e(TAG,"onPayFinish,errCode="+resp.errCode);

		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
			if(resp.errCode == 0){
				AlertDialog.Builder builder = new AlertDialog.Builder(this);
				builder.setTitle("55555");
				builder.setMessage(resp.errCode);
				builder.show();
				tv.setText("支付成功");
				iv.setImageResource(R.mipmap.pay_sucess);

			}else {
				tv.setText("支付失败");
				iv.setImageResource(R.mipmap.pay_failed);
				btn.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View view) {
						finish();
					}
				});
			}
			btn.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					finish();
				}
			});
		}else {
			WXPayEntryActivity.this.finish();
		}

	}
}